﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MUD.Lib.Objects.Users;
using MUD.Lib.Utils;
using MUD.Lib.Objects;
using MUD.Lib.Objects.Rooms;

namespace MUD.Lib.Commands
{
    /// <summary>
    /// Most complicated and most important command in the MUD.
    /// </summary>
    internal class look : ICommand
    {

        #region ICommand Members

        public void Execute(User user, string command)
        {
            // we're looking at our environment
            if (command.IndexOf(' ') < 0)
            {
                user.Write(BuildEnvironmentDescription(user));
            }
            // we're looking at SOMETHING
            else
            {
                // what is that something?
                string something = command.Slice(1, null);

                // Order of operations here is: Character's inventory, environment inventory
                //  player should be able to look at anything with an ID in their inventory or current
                //  environment.

                if (user.CurrentCharacter != null && !user.CurrentCharacter.Inventory.Empty)
                {
                    // ok, there's stuff in the player's current character's inventory.
                    foreach(MudObject obj in user.CurrentCharacter.Inventory.ItemList)
                    {
                        if(obj.ID.Contains(something))
                        {
                            user.Write(obj.LongDesc);
                            return;
                        }
                    }
                }
                else
                {
                    // three possibilities for getting this far:
                    //   - the user doesn't have an active character
                    //   - the character doesn't have anything in their inventory
                    //   - the character's inventory didn't contain anything matching the ID given
                    // So, now we move on to check the user's environment.
                    Room env = null;
                    if (user.CurrentCharacter != null)
                    {
                        env = (Room)user.CurrentCharacter.Environment;
                    }
                    else if (user.Environment != null)
                    {
                        env = (Room)user.Environment;
                    }
                    foreach (MudObject obj in env.ItemList)
                    {
                        if (obj.ID.Contains(something))
                        {
                            user.Write(obj.LongDesc);
                            return;
                        }
                    }
                
                }
                // Ok, nothing was found (or we'd have returned out of the method)
                user.Write(Strings.ObjectNotFound);
            }
        }

        private string BuildEnvironmentDescription(User user)
        {
            StringBuilder sb = new StringBuilder();
            Room env = null;
            if (user.CurrentCharacter != null)
            {
                env = (Room)user.CurrentCharacter.Environment;
            }
            else if (user.Environment != null)
            {
                env = (Room)user.Environment;
            }
            string nl = "\r\n";
            // short description of the room
            sb.Append(nl + "%^YELLOW%^[" + env.ShortDesc + "]%^RESET%^" + nl);
            // long description of the room
            sb.Append(env.LongDesc + nl + nl);
            // tell the user about exits
            sb.Append("%^GREEN%^Obvious Exits: " + env.ExitDirs.NandN() + ".%^RESET%^" + nl + nl);

            // What's in this room?
            foreach (MudObject obj in env.ItemList)
            {
                sb.Append(obj.ShortDesc + nl);
            }

            return sb.ToString();
        }

        public string Help()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
